package com.derbysoft.dswitch.adapter.ihg.webservice.protocal_test;

import java.io.*;

public abstract class IOUtils {

    private static final int INT1024 = 1024;

    private static final int INT4 = 4;

    public static String readAsString(Reader reader) throws IOException {
        StringWriter stringWriter = new StringWriter();
        try {
            BufferedReader bufferedReader = new BufferedReader(reader);
            char[] buffer = new char[INT1024 * INT4];
            for (int length = bufferedReader.read(buffer); length > 0; length = bufferedReader.read(buffer)) {
                stringWriter.write(buffer, 0, length);
            }
            return stringWriter.toString();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } finally {
                if (stringWriter != null) {
                    stringWriter.close();
                }
            }
        }
    }

    public static String readAsString(InputStream inputStream, String charsetName) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[INT1024 * INT4];
            for (int length = inputStream.read(buffer); length > 0; length = inputStream.read(buffer)) {
                outputStream.write(buffer, 0, length);
            }
            return new String(outputStream.toByteArray(), charsetName);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } finally {
                if (outputStream != null) {
                    outputStream.close();
                }
            }
        }
    }

}